package com.xf;


import com.xf.common.Invocation;
import com.xf.protocol.HttpServer;
import com.xf.rigister.LocalRigister;
import com.xf.rigister.Rigister;


import java.io.IOException;

public class Provider {
    public static void main(String[] args) throws IOException, InterruptedException {
        if (args.length != 8) {
            System.out.println("使用示例: java -jar ConfigApp.jar -r <rigister> -p1 <port1> -s <server> -p2 <port2>");
            return;
        }
        String RigistAddress=null;//注册中心ip地址
        Integer port1=null;//注册中心开放给服务器的端口
        Integer port2=null;//服务器接受客户端请求的端口
        String ServerAddress=null;//服务器ip地址
        for (int i = 0; i < args.length; i++) {
            switch (args[i]) {
                case "-r":
                    if (i + 1 < args.length) {
                        RigistAddress = args[++i];
                    } else {
                        System.out.println("错误: 没有提供注册中心ip地址");
                        return;
                    }
                    break;
                case "-p1":
                    if (i + 1 < args.length) {
                        try {
                            port1 = Integer.parseInt(args[++i]);
                        } catch (NumberFormatException e) {
                            System.out.println("错误: 端口号1必须是整数");
                            return;
                        }
                    } else {
                        System.out.println("错误: 没有提供端口号1");
                        return;
                    }
                    break;
                case "-s":
                    if (i + 1 < args.length) {
                        ServerAddress = args[++i];
                    } else {
                        System.out.println("错误: 没有提供服务器ip地址");
                        return;
                    }
                    break;
                case "-p2":
                    if (i + 1 < args.length) {
                        try {
                            port2 = Integer.parseInt(args[++i]);
                        } catch (NumberFormatException e) {
                            System.out.println("错误: 端口号2必须是整数");
                            return;
                        }
                    } else {
                        System.out.println("错误: 没有提供端口号2");
                        return;
                    }
                    break;
                default:
                    System.out.println("未知命令: " + args[i]);
                    return;
            }
        }

        //服务注册
        System.out.println("服务器启动成功");
        System.out.println("服务器注册服务中。。。。");
        Invocation invocation0 = new Invocation(HelloService.class.getName(),"sayingHello",
                new Class[]{String.class},new Object[]{},"1.0");
        Rigister.rigist(RigistAddress,port1,invocation0,ServerAddress,port2);
        LocalRigister.rigist(invocation0.getInterfaceName()+invocation0.getVersion(), HelloServiceImpl_V1.class);

        Invocation invocation2 = new Invocation(HelloService.class.getName(),"sayingHello",
                new Class[]{String.class},new Object[]{},"2.0");
        Rigister.rigist(RigistAddress,port1,invocation2,ServerAddress,port2);
        LocalRigister.rigist(invocation2.getInterfaceName()+invocation2.getVersion(), HelloServiceImpl_V2.class);

        Invocation invocation1 = new Invocation(Arithmetic.class.getName(), "add",
                new Class[]{Double.class, Double.class}, new Object[]{}, "1.0");
        Rigister.rigist(RigistAddress,port1,invocation1,ServerAddress,port2);
        invocation1 = new Invocation(Arithmetic.class.getName(), "sub",
                new Class[]{Double.class, Double.class}, new Object[]{}, "1.0");
        Rigister.rigist(RigistAddress,port1,invocation1,ServerAddress,port2);
        invocation1 = new Invocation(Arithmetic.class.getName(), "mul",
                new Class[]{Double.class, Double.class}, new Object[]{}, "1.0");
        Rigister.rigist(RigistAddress,port1,invocation1,ServerAddress,port2);
        invocation1 = new Invocation(Arithmetic.class.getName(), "div",
                new Class[]{Double.class, Double.class}, new Object[]{}, "1.0");
        Rigister.rigist(RigistAddress,port1,invocation1,ServerAddress,port2);
        LocalRigister.rigist(invocation1.getInterfaceName()+invocation1.getVersion(),ArithmeticImpl.class);

        Invocation invocation3 = new Invocation(StringMethod.class.getName(), "turn_up",
                new Class[]{String.class}, new Object[]{}, "1.0");
        Rigister.rigist(RigistAddress,port1,invocation3,ServerAddress,port2);
        invocation3 = new Invocation(StringMethod.class.getName(), "turn_low",
                new Class[]{String.class}, new Object[]{}, "1.0");
        Rigister.rigist(RigistAddress,port1,invocation3,ServerAddress,port2);
        invocation3 = new Invocation(StringMethod.class.getName(), "reversal",
                new Class[]{String.class}, new Object[]{}, "1.0");
        Rigister.rigist(RigistAddress,port1,invocation3,ServerAddress,port2);
        invocation3 = new Invocation(StringMethod.class.getName(), "count_num",
                new Class[]{String.class, Character.class}, new Object[]{}, "1.0");
        Rigister.rigist(RigistAddress,port1,invocation3,ServerAddress,port2);
        LocalRigister.rigist(invocation3.getInterfaceName()+invocation3.getVersion(),StringMethodImpl.class);

        System.out.println("服务注册成功！");
        System.out.println("开始监听客户端请求。");

        //处理客户端请求
        HttpServer httpServer = new HttpServer();
        httpServer.start_for_provider(RigistAddress,port1,ServerAddress,port2);


    }
}
